﻿using FluentValidation;
using Mes.Domain.BoundedContext;
using Mes.Application.Services.MockData;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.DTO.Results.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    /// <summary>
    /// 物料控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class MaterialController : ControllerBase
    {
        private readonly IMaterialService _materialService;
        private readonly IValidator<AddMaterialInput> _addMaterialInputValidator;
        private readonly IValidator<GetMaterialsInput> _getMaterialsInputValidator;
        private readonly IValidator<UpdateMaterialInput> _updateMaterialInputValidator;
        private readonly MockDataService _mockDataService;
        /// <summary>
        /// 物料控制器
        /// </summary>
        /// <param name="materialService"></param>
        /// <param name="addMaterialInputValidator"></param>
        /// <param name="getMaterialsInputValidator"></param>
        /// <param name="updateMaterialInputValidator"></param>
        /// <param name="mockDataService"></param>
        public MaterialController(IMaterialService materialService,
            IValidator<AddMaterialInput> addMaterialInputValidator,
            IValidator<GetMaterialsInput> getMaterialsInputValidator,
            IValidator<UpdateMaterialInput> updateMaterialInputValidator,
            MockDataService mockDataService
            )
        {
            _materialService = materialService;
            _addMaterialInputValidator = addMaterialInputValidator;
            _getMaterialsInputValidator = getMaterialsInputValidator;
            _updateMaterialInputValidator = updateMaterialInputValidator;
            _mockDataService = mockDataService;
        }

        /// <summary>
        /// 新增物料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("material")]
        public async Task<ActionResult> AddMaterialAsync([FromBody] AddMaterialInput input)
        {
            try
            {
                var result = await _addMaterialInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _materialService.AddMaterialAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑物料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("material")]
        public async Task<ActionResult> UpdateMaterialAsync([FromBody] UpdateMaterialInput input)
        {
            try
            {
                var result = await _updateMaterialInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _materialService.UpdateMaterialAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除物料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("materials")]
        public async Task<ActionResult> DeleteMaterialsAsync(IList<int> ids)
        {
            try
            {
                await _materialService.DeleteMaterialsAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询物料分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("materials")]
        public async Task<ActionResult<PagedItemsData<MaterialsResults>>> GetMaterialsAsync([FromQuery] GetMaterialsInput input)
        {
            try
            {
                var result = await _getMaterialsInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var materials = await _materialService.GetMaterialsAsync(input);
                return Ok(materials);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询物料实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MaterialResults</returns>
        [HttpGet("material")]
        public async Task<ActionResult<MaterialResults>> GetMaterialAsync(int id)
        {
            try
            {
                var response = await _materialService.GetMaterialAsync(id);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询物料实体测试
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MaterialResults</returns>
        [HttpGet("materialtest")]
        public ActionResult<MaterialResults> GetMaterialTestAsync(int id)
        {
            try
            {
                var data = _mockDataService.Mock<MaterialResults>();
                return new JsonResult(data);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
